﻿#include "zuhechaxun.h"
#include "ui_zuhechaxun.h"
#include <QDebug>
#include <QSqlQuery>
#include <QTextCodec>
#include <QMessageBox>

int ZuHeChaXun::flog=0;

ZuHeChaXun::ZuHeChaXun(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ZuHeChaXun)
{
    ui->setupUi(this);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    //ui.scrollAreaWidgetContents->adjustSize();

    //时间设置
    ui->renzhis->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->renzhis->setCalendarPopup(true);
    ui->renzhie->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->renzhie->setCalendarPopup(true);
    ui->births->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->births->setCalendarPopup(true);
    ui->birthe->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->birthe->setCalendarPopup(true);

    ui->rudangs->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->rudangs->setCalendarPopup(true);
    ui->rudange->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->rudange->setCalendarPopup(true);
    ui->joinworks->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->joinworks->setCalendarPopup(true);
    ui->joinworke->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
    ui->joinworke->setCalendarPopup(true);

}
ZuHeChaXun::~ZuHeChaXun()
{
    delete ui;

}

void ZuHeChaXun::closeEvent(QCloseEvent*event)
{

    ZuHeChaXun::flog=0;
    event->accept();
}
//双击添加
void ZuHeChaXun::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    if(item->parent())
    {
        QTreeWidgetItem *parent=new QTreeWidgetItem(ui->treeWidget_2);
        parent->setText(column,item->parent()->text(0));
        QTreeWidgetItem *child=new QTreeWidgetItem(parent);
        child->setText(column,item->text(0));
        ui->treeWidget_2->expandItem(parent);
    }



}

//双击删除
void ZuHeChaXun::on_treeWidget_2_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    if(item->parent())
    {
        delete item->parent();
    }
}

//左侧条件筛选-参数查找
void ZuHeChaXun::on_buttonFind_1_clicked()
{
    QString coFilter = ui->lineEdit->text();
    QTreeWidgetItemIterator it(ui->treeWidget);
        while (*it)
        {
            if((*it)->text(0).contains(coFilter))
            {
                (*it)->setHidden(false);
                QTreeWidgetItem *item = *it;
                while (item->parent())
                {
                    item->parent()->setHidden(false);

                    item = item->parent();
                }
            }
            else
            {
                (*it)->setHidden(true);
            }
            ++it;
        }
}

//右侧条件筛选-参数查找
void ZuHeChaXun::on_buttonFind_2_clicked()
{
    QString coFilter = ui->lineEdit_2->text();
    QTreeWidgetItemIterator it(ui->treeWidget_2);
        while (*it)
        {
            if((*it)->text(0).contains(coFilter))
            {
                (*it)->setHidden(false);
                QTreeWidgetItem *item = *it;

                while (item->parent())
                {
                    item->parent()->setHidden(false);

                    item = item->parent();
                }
            }
            else
            {
                (*it)->setHidden(true);
            }
            ++it;
        }
}


void ZuHeChaXun::on_pushButton_clicked()//连接重名干部信号槽
{
    this->close();
    same=new samename();
    connect(same,SIGNAL(sendData1(QString)),this,SLOT(recvData1(QString)));

    if(same->exec()==QDialog::Accepted){
    }
    delete same;
    //    same->show();


}

void ZuHeChaXun::recvData1(QString id)//接收重名干部
{
    emit sendData2(id);
}

void ZuHeChaXun::on_pushButton_3_clicked()//发送一个根据条件查询后的干部IDNum
{
    QVector<QString> ljt;
    QVector<QString> wtm;
    QString id="NULL";
    QString judgeall;
    QString name;
    bool namenotempty=!ui->name->text().isEmpty();
    QString unit;
    bool unitnotempty=!ui->unit->text().isEmpty();
    QString zhiwu;
    bool zhiwunotempty=!ui->zhiwu->text().isEmpty();
    QString schooledu;
    bool schooledunotempty=!ui->schooledu->text().isEmpty();
    QString workedu;
    bool workedunotempty=!ui->workedu->text().isEmpty();

    QString birth;
    QString births=ui->births->text();                       //出生日期start
    QString birthe=ui->birthe->text();                       //出生日期end

    QString joinworktime;
    QString joinworks=ui->joinworks->text();
    QString joinworke=ui->joinworke->text();

    QString rudangtime;
    QString rudangs=ui->rudangs->text();
    QString rudange=ui->rudange->text();

    QString renzhitime;
    QString renzhis=ui->renzhis->text();
    QString renzhie=ui->renzhie->text();

    QSqlQuery selectall;
    QSqlQuery changejiguan;
    judgeall=QString("select distinct c.IDNum from CadreInfo c,unit u,Post p,Edu e where c.IDNum=p.IDNum and c.IDNum=e.IDNum and c.UnitID=u.unitid ");

    QString jiguan;
    QString jiguanname;
    QString jiguanshengid;
    bool jiguannotempty=!ui->jiguanplace->text().isEmpty();
    if(jiguannotempty){
        if(ui->comboBox->currentText()=="属于"){
            jiguanname=ui->jiguanplace->text();
            jiguanname=jiguanname+"省";
            changejiguan.exec(QString("select SUBSTRING(AdminAreaID,1,2) from AdminAreaCode where AdminArea like '%%1%'").arg(jiguanname));
            while(changejiguan.next()){
                jiguanshengid=changejiguan.value(0).toString();
                jiguan=QString(" AND SUBSTRING(c.NativePlace,1,2)='%1' ").arg(jiguanshengid);
            }
        }
         if(ui->comboBox->currentText()=="不属于"){
             jiguanname=ui->jiguanplace->text();
             jiguanname=jiguanname+"省";
             changejiguan.exec(QString("select SUBSTRING(AdminAreaID,1,2) from AdminAreaCode where AdminArea like '%%1%'").arg(jiguanname));
             while(changejiguan.next()){
                 jiguanshengid=changejiguan.value(0).toString();

                 jiguan=QString(" AND SUBSTRING(c.NativePlace,1,2)!='%1' ").arg(jiguanshengid);
             }
         }
    }

    QString birthplace;
    QString birthplacename;
    bool birthplacenotempty=!ui->birthplace->text().isEmpty();
    if(birthplacenotempty){
        birthplacename=ui->birthplace->text();
        birthplacename=birthplacename+"省";
        if(ui->comboBox_2->currentText()=="属于"){
            birthplace=QString(" AND BirthPlace like '%%1%' ").arg(birthplacename);
        }
        if(ui->comboBox_2->currentText()=="不属于"){
            birthplace=QString(" AND BirthPlace not like '%%1%' ").arg(birthplacename);
        }
    }

    QString growplace;
    QString growplacename;
    bool growplacenotempty=!ui->growplace->text().isEmpty();
    if(growplacenotempty){
        growplacename=ui->growplace->text();
        growplacename=growplacename+"省";
        if(ui->comboBox_3->currentText()=="属于"){
            growplace=QString(" AND GrowPlace like '%%1%' ").arg(growplacename);
        }
        if(ui->comboBox_3->currentText()=="不属于"){
            growplace=QString(" AND GrowPlace not like '%%1%' ").arg(growplacename);
        }
    }
    QString status;//干部状态
    QString threeone; //三龄一历
    if(ui->ischeckBox->isChecked()&&!ui->notcheckBox->isChecked()){
        threeone=" AND c.ThreeOne='0'";
    }
    if(ui->notcheckBox->isChecked()&&!ui->ischeckBox->isChecked()){
        threeone=" AND c.ThreeOne='1'";
    }

    if(ui->zaizhicheckBox->isChecked()&&!ui->lituicheckBox->isChecked()&&!ui->others->isChecked()){
        status=QString(" AND c.ManageStatusID='%1' ").arg("1");//在职
    }
    if(ui->lituicheckBox->isChecked()&&!ui->others->isChecked()&&!ui->zaizhicheckBox->isChecked()){
        status=QString(" AND c.ManageStatusID='%1' ").arg("2");//离退
    }
    if(ui->others->isChecked()&&!ui->zaizhicheckBox->isChecked()&&!ui->lituicheckBox->isChecked()){
        status=QString(" AND c.ManageStatusID='%1' ").arg("9");//其他
    }
    //选三个
    if(ui->zaizhicheckBox->isChecked()&&ui->lituicheckBox->isChecked()&&ui->others->isChecked()){
        status=" AND (c.ManageStatusID='1' or c.ManageStatusID='2' or c.ManageStatusID='9')";
    }
    //选两个
    if(ui->zaizhicheckBox->isChecked()&&ui->lituicheckBox->isChecked()&&!ui->others->isChecked()){
        status=" AND (c.ManageStatusID='1' or c.ManageStatusID='2')";
    }
    if(ui->zaizhicheckBox->isChecked()&&!ui->lituicheckBox->isChecked()&&ui->others->isChecked()){
        status=" AND (c.ManageStatusID='1' or c.ManageStatusID='9')";
    }
    if(!ui->zaizhicheckBox->isChecked()&&ui->lituicheckBox->isChecked()&&ui->others->isChecked()){
        status=" AND (c.ManageStatusID='9' or c.ManageStatusID='2')";
    }

    if(unitnotempty){
        unit=QString(" AND u.unitname='%1'").arg(ui->unit->text());
    }
    if(zhiwunotempty){
        zhiwu=QString(" and p.PostName='%1' ").arg(ui->zhiwu->text());
    }
    if(namenotempty){
        name=QString(" AND c.CadreName like '%%1%' ").arg(ui->name->text());
    }

    //专业表二选一
    if(schooledunotempty){
        schooledu=QString(" AND e.Major='%1' and e.WorkORSchool=1 ").arg(ui->schooledu->text());
    }
    if(workedunotempty){
        workedu=QString(" AND e.Major='%1' and e.WorkORSchool=0 ").arg(ui->workedu->text());
    }

    birth=QString(" and c.Birth>'%1' and c.Birth<'%2'").arg(births).arg(birthe);
    joinworktime=QString(" and c.JoinWorkTime>'%1' and c.JoinWorkTime<'%2'").arg(joinworks).arg(joinworke);

    rudangtime=QString(" and c.PartyTime>'%1' and c.PartyTime<'%2'").arg(rudangs).arg(rudange);
    renzhitime=QString(" and p.PostTime>'%1' and p.PostTime<'%2' and p.QualifyType=0").arg(renzhis).arg(renzhie);

    if(!ui->rudangcheck->isChecked()){
        rudangtime="";
    }
    if(!ui->joinworkcheck->isChecked()){
        joinworktime="";
    }
    if(!ui->renzhicheck->isChecked()){
        renzhitime="";
    }
    judgeall=judgeall+zhiwu+name+unit+workedu+schooledu+birth+joinworktime+rudangtime+renzhitime+status+threeone+jiguan+birthplace+growplace;
    qDebug()<<judgeall;
    selectall.exec(judgeall);
    while(selectall.next()){
        id=selectall.value(0).toString();
//        qDebug()<<id;
        ljt.append(id);
//        emit sendData3(id);//发送查询的干部IDNum

//        this->close();

    }
//qDebug()<<ljt;
    //天美部分
//****************************************************************************************
    QString sqlAll;
    QSqlQuery queryAll;
    QSqlQuery queryNation;
    QSqlQuery queryPolitic;
    QSqlQuery queryManageStatus;
    QSqlQuery queryRank;

    QString birthS = ui->births->text();
    QString birthE = ui->birthe->text();
    QString birthScale = QString(" Birth >= '%1' and Birth <= '%2'").arg(birthS).arg(birthE);

    QString idNum="NULL";
    QString sex;
    QString nation;
    QString politic;
    QString threeOne;
    QString idenCate;
    QString manageStatus;
    QString rank;

    QTreeWidgetItemIterator it(ui->treeWidget_2);
    while (*it)
    {
        if((*it)->text(0)=="性别")
        {

            if((*it)->child(0)->text(0)=="男"){
                sex = QString(" and Sex = '%1'").arg(1);
            }
            else if((*it)->child(0)->text(0)=="女"){
                sex = QString(" and Sex = '%1'").arg(0);
            }
        }
        if((*it)->text(0)=="民族")
        {
            QString nationCode;
            queryNation.exec(QString("select NationID from NationCode where Nation = '%1'").arg((*it)->child(0)->text(0)));
            while(queryNation.next())
            {
                nationCode = queryNation.value(0).toString();
                //qDebug()<<nationCode;
            }
            nation = QString(" and NationID = '%1'").arg(nationCode);

        }
        if((*it)->text(0)=="政治面貌")
        {
            QString politicCode;
            queryPolitic.exec(QString("select PoliticID from PoliticCode where Politic = '%1'").arg((*it)->child(0)->text(0)));
            while(queryPolitic.next())
            {
                politicCode = queryPolitic.value(0).toString();
                //qDebug()<<politicCode;
            }
            politic = QString(" and PoliticID = '%1'").arg(politicCode);

        }
        if((*it)->text(0)=="已认定三龄一历")
        {
            if((*it)->child(0)->text(0) == "是")
            {
                threeOne = QString(" and ThreeOne = '1'");
            }
            else
            {
                threeOne = QString(" and ThreeOne = '0'");
            }

        }
        if((*it)->text(0)=="干部身份")
        {
            idenCate = QString(" and IdenCate = '%1'").arg((*it)->child(0)->text(0));

        }
        if((*it)->text(0)=="干部状态")
        {
            QString manageStatusCode;
            queryManageStatus.exec(QString("select ManageStatusID from ManageStatusCode where ManageStatus = '%1'").arg((*it)->child(0)->text(0)));
            while(queryManageStatus.next())
            {
                manageStatusCode = queryManageStatus.value(0).toString();
                //qDebug()<<manageStatusCode;
            }
            manageStatus = QString(" and ManageStatusID = '%1'").arg(manageStatusCode);

        }
        if((*it)->text(0)=="职务级别")
        {
            QString rankCode;
            queryRank.exec(QString("select RankID from RankCode where RankName = '%1'").arg((*it)->child(0)->text(0)));
            while(queryRank.next())
            {
                rankCode = queryRank.value(0).toString();
                //qDebug()<<rankCode;
            }
            rank = QString(" and RankID = '%1'").arg(rankCode);

        }

        ++it;
    }
    sqlAll = QString("select IDNum from CadreInfo where");
    sqlAll = sqlAll + birthScale + sex + nation + threeOne + idenCate + manageStatus + rank;
    qDebug()<<sqlAll;
    queryAll.exec(sqlAll);
    while(queryAll.next())
    {
        idNum = queryAll.value(0).toString();
        wtm.append(idNum);
    }

//***********************************************************************************************
    for(int i = 0; i < ljt.count(); i++){

        for(int j = 0; j < wtm.count(); j++){

            if(ljt[i]==wtm[j]){
                        emit sendData3(wtm[j]);//发送查询的干部IDNum
                        this->close();
            }
        }
    }
    if(id=="NULL"){
        QMessageBox::information(this, tr("提示信息"), tr("系统中未找到查询干部的信息"));
    }
    else if(idNum=="NULL"){
        QMessageBox::information(this, tr("提示信息"), tr("系统中未找到查询干部的信息"));
    }
    ZuHeChaXun::flog=0;
}

void ZuHeChaXun::on_oldmin_textChanged(const QString &arg1)//最小年龄关联出生日期
{
    int oldmin=-arg1.toInt();

    QDate now=QDate::currentDate();

    QDate max=now.addYears(oldmin);

    ui->birthe->setDate(max);
}

void ZuHeChaXun::on_oldmax_textChanged(const QString &arg1)//最大年龄关联出生日期
{
    int oldmax=-arg1.toInt();

    QDate now=QDate::currentDate();

    QDate min=now.addYears(oldmax);

    ui->births->setDate(min);
}

void ZuHeChaXun::on_renzhimin_textChanged(const QString &arg1)//任职最小年限关联任职截止时间
{
    int renzhimin=-arg1.toInt();

    QDate now=QDate::currentDate();

    QDate max=now.addYears(renzhimin);

    ui->renzhie->setDate(max);
}

void ZuHeChaXun::on_renzhimax_textChanged(const QString &arg1)//任职最大年限关联任职起始时间
{
    int renzhimax=-arg1.toInt();

    QDate now=QDate::currentDate();

    QDate min=now.addYears(renzhimax);

    ui->renzhis->setDate(min);
}

